//
//  EFSWKWebViewController+CommonEvent.m
//  LuGong
//
//  Created by zhuangdechao on 2017/7/18.
//  Copyright © 2017年 YiShi. All rights reserved.
//

#import "EFSWKWebViewController+CommonEvent.h"
#import "EFSRefreshWebViewController.h"
#import <ReactiveCocoa/RACEXTScope.h>
#import "UIViewController+Visible.h"
#import "EFSActivityWebController.h"


NSString *const kRefreshList = @"ytx:list:refresh"; //下拉刷新 以及 初始数据注入
NSString *const kLoadMore = @"ytx:list:more"; //加载更多
NSString *const kNavigate = @"ytx:navigate"; //页面跳转
NSString *const kUserInfoInject = @"ytx:inject"; //用户信息注入
NSString *const kUserPhoneBound = @"ytx:phoneBound"; //用户完成手机绑定
NSString *const kUserAnalytics = @"ytx:analytics";//用户数据统计
NSString *const kUserShare = @"ytx:share"; //用户分享
NSString *const kOpenAccount = @"ytx:openAccountDeal"; //NFXG开户
NSString *const kNFXGSinaPayIncome = @"ytx:sinaPayIncome"; //转入跳转
NSString *const kGoVideo = @"ytx:goVideo";
NSString *const kGetGoodId = @"ytx:goodsid";

@implementation EFSWKWebViewController (CommonEvent)
- (void)refreshWithData:(id)data
{
    [self.bridge callHandler:kRefreshList data:data];
}

- (void)injectUserInfo
{
    //    id user = [MTLJSONAdapter JSONDictionaryFromModel:[EFSUserManager sharedManager].currentUser];
    id user;
    [self.bridge callHandler:kUserInfoInject data:@{@"name":@"zdc"}];
}

//跳转视频
- (void)handlerGoVideo{
    [self.bridge registerHandler:kGoVideo handler:^(id data, WVJBResponseCallback responseCallback) {
        [UIViewController visibleViewController].tabBarController.selectedIndex = 2;
    }];
}





- (void)handlerNavigate
{
    @weakify(self)
    [self.bridge registerHandler:kNavigate handler:^(id data, WVJBResponseCallback responseCallback) {
        @strongify(self)
        NSInteger type = [[data valueForKey:@"type"] integerValue];
        id detail = data[@"detail"];
        
        if(detail[@"id"])
        {
            
        }
        
        NSString* url = detail[@"url"];
        
        if (url) {
            
            if(type == 100000)
            {
                //                if ([[UIViewController visibleViewController] isKindOfClass:[UIViewController class]] && ![[UIViewController visibleViewController] isMemberOfClass:[EFSLiveViewController class]]) {
                //                    EFSLiveViewController * viewController = [[EFSLiveViewController alloc] init];
                //                    viewController.isChangeTitle = YES;
                //                    [self.navigationController pushViewController:viewController animated:YES];
                //                }
            }else if (type == 3)
            {
                //                if ([[UIViewController visibleViewController] isKindOfClass:[UIViewController class]] && ![[UIViewController visibleViewController] isMemberOfClass:[EFSLearnWebViewController class]] && url) {
                //                    EFSLearnWebViewController * viewController = [[EFSLearnWebViewController alloc]init];
                //                    viewController.configUrl = url;
                //                    viewController.isNavbarShow= YES;
                //                    [self.navigationController pushViewController:viewController animated:YES];
                //                }
            }
            else
            {
                if ([[UIViewController visibleViewController] isKindOfClass:[UIViewController class]]) {
                    EFSRefreshWebViewController * viewController = [[EFSRefreshWebViewController alloc] init];
                    viewController.configUrl = url;
                    [self.navigationController pushViewController:viewController animated:YES];
                }
            }
        }
        else {
            NSMutableDictionary *detailDict;
            if (detail == nil) {
                detailDict = [@{@"hidenNav": @(NO)} mutableCopy];
            }else if ([detail isKindOfClass:[NSDictionary class]]) {
                detailDict = [detail mutableCopy];
                detailDict[@"hidenNav"] = @(NO);
            }
            if ([[UIViewController visibleViewController] isKindOfClass:[UIViewController class]]) {
                //                [EFSJump jump:type withData:detailDict from:self];
            }
        }
    }];
}

- (void)handlerLoadMore:(WVJBHandler)callback
{
    [self.bridge registerHandler:kLoadMore handler: callback];
}

- (void)handlerUserPhoneBound
{
    [self.bridge registerHandler:kUserPhoneBound handler:^(id data, WVJBResponseCallback responseCallback) {
        //        [[EFSUserManager sharedManager] userBoundPhoneFromOtherChannel];
    }];
}

- (void)handlerUserAnalytics
{
    [self.bridge registerHandler:kUserAnalytics handler:^(id data, WVJBResponseCallback responseCallback) {
        //@TODO("EFSAnalytics");
        //        NSString *step = data[@"step"];
        //        [YTXAnalytics page:[NSString stringWithFormat:@"%@%@", OPENACC_STEP, step]
        //            withData:@{
        //                      @"server":data[@"server"],
        //                      @"info":data[@"info"],
        //                      @"sourceType":[[YTXAnalytics sharedAnalytics] sourceType],
        //                      @"sourceId":[[YTXAnalytics sharedAnalytics] sourceId]}];
    }];
}

- (void)handlerShare:(WVJBHandler)callback
{
    [self.bridge registerHandler:kUserShare handler:callback];
}

- (void)handlerNFXGSinaPay
{
    //    @weakify(self)
    //    [self.bridge registerHandler:kNFXGSinaPayIncome handler:^(id data, WVJBResponseCallback responseCallback) {
    //        @strongify(self)
    //        @TODO("交易待改");
    //        JumpType type = [EFSNFXGTradeManager sharedManager].tradeToken ? 1:JumpTypeNFXGTradeLogin;
    
    //        [EFSJump jump:type withData:nil from:self];
    //    }];
}


@end
